package world.snowcrystal.configuration;

import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.actuate.autoconfigure.endpoint.web.CorsEndpointProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Log4j2
@Configuration
@EnableWebMvc
@EnableConfigurationProperties(CorsEndpointProperties.class)
public class WebMvcConfiguration implements WebMvcConfigurer {


    //    @Bean
//    public CorsConfiguration globalCorsConfiguration(CorsEndpointProperties props) {
//        CorsConfiguration config = new CorsConfiguration();
//        config.addAllowedMethod(HttpMethod.GET);
//        config.addAllowedMethod(HttpMethod.POST);
//        config.addAllowedMethod(HttpMethod.OPTIONS);
//        for(var s :props.getAllowedHeaders()){
//            config.addAllowedHeader(s);
//        }
////        config.addAllowedHeader("Origin");
////        config.addAllowedHeader("No-Cache");
////        config.addAllowedHeader("X-Requested-With");
////        config.addAllowedHeader("If-Modified-Since");
////        config.addAllowedHeader("Pragma");
////        config.addAllowedHeader("Last-Modified");
////        config.addAllowedHeader("Cache-Control");
////        config.addAllowedHeader("Expires");
////        config.addAllowedHeader("Content-Type");
////        config.addAllowedHeader("Access-Control-Allow-Credentials");
////        config.addAllowedHeader("Keep-Alive");
////        config.addAllowedHeader("User-Agent");
////        config.addAllowedHeader("Range");
//        config.addAllowedOrigin("*");
//        return config;
//    }
    @Bean
    public CorsConfiguration credentialsAllowedCorsConfiguration(CorsEndpointProperties props) {
        CorsConfiguration config = new CorsConfiguration();
        config.addAllowedMethod(HttpMethod.GET);
        config.addAllowedMethod(HttpMethod.POST);
        config.addAllowedMethod(HttpMethod.PUT);
        config.addAllowedMethod(HttpMethod.HEAD);
        config.addAllowedMethod(HttpMethod.DELETE);
        config.addAllowedMethod(HttpMethod.OPTIONS);
        for (var s : props.getAllowedHeaders()) {
            config.addAllowedHeader(s);
        }
        config.setAllowedOriginPatterns(props.getAllowedOriginPatterns());
        config.setAllowCredentials(props.getAllowCredentials());
        return config;
    }

    @Bean(destroyMethod = "destroy")
    public CorsFilter corsFilter(@Qualifier("credentialsAllowedCorsConfiguration") CorsConfiguration credentialsAllowedCorsConfiguration
    ) {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", credentialsAllowedCorsConfiguration);
        return new CorsFilter(source);
    }


}
